<?php

namespace app\controller\warehouse;

use app\Auth;
use app\BaseController;
use lc\helpers\Response;
use think\facade\Db;


class Sorting extends BaseController
{


    /**
     * 搜索返回订单信息
     */
    public function selectOrder()
    {

        $orderId = $_REQUEST['orderId'];

        $orderData = Db::table('xd_store_order')->field('id,order_id,uid,real_name,user_phone,mark,remark')->where('order_id', $orderId)->find();

        //获取订单商品信息
        if (!empty($orderData)) {

            $product = Db::table('xd_store_order_cart_info')->where('oid', $orderData['id'])->select()->toArray();

            if (!empty($product)) {

                foreach ($product as $k => $v) {
                    $cartInfo = json_decode($v['cart_info'], true);

                    $productData[$k]['name'] = $cartInfo['productInfo']['store_name'];
                    $productData[$k]['image'] = $cartInfo['productInfo']['image'];

                }
                $orderData['productData'] = $productData;

                return response::success($orderData);

            } else {

                return response::fail('商品信息查找失败');
            }

        } else {
            return response::fail('暂未找到该订单');
        }
    }


    /**通过订单获取衣物信息
     * @return void
     */
    public function getGoodsInfo()
    {

        $oId = $_REQUEST['oId'];

        $data = Db::table('xd_store_order_goodsinfo')->where('oId', $oId)->select()->toArray();

        return response::success($data);

    }


    /**
     * 添加衣物
     */

    public function addGoodsInfo()
    {

        $adminId = Auth::$adminId;
        $oId = $_REQUEST['oId'];

        $id = Db::table('xd_store_order_goodsinfo')->insertGetId([
            'goods_name' => '任意洗',
            'admin_id' => $adminId,
            'oid' => $oId,
            'status' => 1,
            'create_time' => date('Y-m-d H:i:s', time())
        ]);

        return response::success($id);
    }


    /**
     * 删除衣物
     */

    public function removeGoodsInfo()
    {

        $adminId = Auth::$adminId;
        $id = $_REQUEST['id'];

        Db::table('xd_store_order_goodsinfo')
            ->where('id', $id)
            ->delete();


        return response::success();
    }

    /**
     * 衣物添加属性
     */
    public function addGoodsAtt()
    {

        $adminId = Auth::$adminId;

        $id = $_REQUEST['id'] ?? '';

        $type = $_REQUEST['type'];
        $pic = $_REQUEST['pic'];
        $price = $_REQUEST['price'];
        $title = $_REQUEST['title'];

        if ($id != '') {

            if ($type == 1) {
                //衣服
                $arr = array(
                    'goods_name' => $title,
                    'goods_price' => $price
                );
            } else if ($type == 2) {
                //颜色
                $arr = array(
                    'goods_color' => $title
                );

            } else if ($type == 3) {
                //瑕疵
                //title为数组

                Db::table('xd_store_order_goodsinfo')->where('id',$id)->update([
                    'goods_flaw'=>json_encode($title)
                ]);

                return response::success();

            }else if($type == 4){

                //附件
                Db::table('xd_store_order_goodsinfo')->where('id',$id)->update([
                    'fidjson'=>json_encode($title)
                ]);

                return response::success();

            }else if($type == 5){

                //洗涤要求

                return response::success();
            }else if($type == 6){

                //品牌
                return response::success();
            }else if($type == 7){

                //异常
                return response::success();
            }elseif($type == 8){

                //拍照
                Db::table('xd_store_order_goodsinfo')->where('id',$id)->update([
                    'pic'=>json_encode($pic)
                ]);

            }else{

                return response::fail('参数错误');

            }

            Db::table('xd_store_order_goodsinfo')->where('id',$id)->update($arr);

            return response::success();
        }
    }


    /**
     * 返回商品属性
     */
    public function getGoodsattribute()
    {

        $adminId = Auth::$adminId;
        $type = $_REQUEST['type'];

        if (empty($type)) {
            return response::fail('参数错误');
        }

        $data = Db::table('xd_store_order_goodsattribute')->where('type', $type)->select();

        return response::success($data);
    }


    /**
     * 分拣完成
     */

    public function OrderSorting()
    {

        $adminId = Auth::$adminId;
        $orderId = $_REQUEST['orderId'] ?? '';
        //分配格架号
        //查找当前前置仓已释放的格架号

        $screenwork = Db::table('xd_store_order_goodsattribute')
            ->where('type',10)
            ->where('admin_id',$adminId)
            ->where('status',1)
            ->find();


        if(empty($screenwork)){
            return response::fail('当前无空格架');
        }

        //占用格架号

        Db::startTrans();
        try {
            Db::table('xd_store_order_goodsinfo')->where('oid',$orderId)->update(['shelf_no'=>$screenwork['title']]);
            Db::table('xd_store_order_goodsattribute')->where('id',$screenwork['id'])->update([
                'status'=>1
            ]);
            Db::table('xd_store_order')->where('id',$orderId)->update([
                'is_sorting'=>1
            ]);
            // 提交事务
            Db::commit();

            return response::success($screenwork['title']);

        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return response::fail('网络错误,请刷新重试');
        }
    }


    /**
     * 返回快递公司
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function resDelivery(){

        $adminId = Auth::$adminId;

        $res = Db::name('delivery')->field('id,delivery_code,delivery_name')->select();

        return response::success($res);
    }


    /**
     * 发货
     */
    public function deliverGoods(){


        $adminId = Auth::$adminId;
        $oId = $_REQUEST['oId'];
        $res = Db::table('xd_store_order')->where('id',$oId)->update([
            'status'=>6
        ]);

        if($res != 0){
            return response::success();
        }else{
            return response::fail('发货失败');
        }

    }



    /**
     *收货
     */

    public function takeGoods(){



    }

    /**
     * 总检or污渍中止
     * type   1 污渍中止标记   2 取消污渍中止标记   3 总检  4 取消总检
     */
    public function goodsStain(){

        $adminId = Auth::$adminId;
        $type = $_REQUEST['type'];
        $id = $_REQUEST['id'];
        //水洗唛

        if($type == 3){

            $update = [
                'status'=>3,
                'zongjiantime'=>date('Y-m-d H:i:s')
            ];
            $msg = '总检失败';

        }else if($type == 4){

            $update = [
                'status'=>2
            ];
            $msg = '取消总检失败';

        }else if($type == 1){

            $update = [
                'termination'=>1
            ];
            $msg = '标记失败';

        }else if($type == 2){

            $update = [
                'termination'=>0
            ];
            $msg = '取消标记失败';

        }else{
            return response::fail('参数错误');
        }

        $upd = Db::table('xd_store_order_goodsInfo')->where('id',$id)->update($update);

        if($upd != 0 ){
            return response::success();
        }

        return response::fail($msg);
    }



    /**
     * 工厂发货
     */



    /**
     * 前置仓到货
     */


    /**
     * 取件
     */















}
